'use strict';
class BaseController {
  constructor(ctx) {
    this.ctx = ctx;
  }

  get ctx_get() {
    return this.ctx.query;
  }

  get ctx_post() {
    return this.ctx.request.body;
  }

  get user() {
    return this.ctx.session.user;
  }

  judge_result(result, err_msg = 'none') {

    if (result.data.length === 0) {
      this.ctx.body = {
        code: 4000,
        msg: err_msg,
        data: result.data,
      };
    } else if (result.data.length > 0) {
      this.ctx.body = {
        code: 2000,
        msg: 'success',
        data: result.data,
      };
    }
  }

  success(data) {
    // this.ctx.body = {
    //   code: 2000,
    //   msg: '操作成功',
    //   data,
    // };
    this.ctx = this.ctx.helper.fomateData(
      {
         msg: '操作成功',
         data
      }, 
      2000)
  }

  formate_data(data) {
    const defaul = {
      code: '5000',
      msg: '操作错误',
    };
    const result = Object.assign(data, defaul);
    return { result };
  }

  errorF(data) {
    this.ctx.body = {
      code: 5000,
      msg: '操作失败',
      data,
    };
  }

  error(data) {
    this.ctx.body = data;
  }

  notFound(msg) {
    msg = msg || 'not found';
    this.ctx.throw(404, msg);
  }

  // 检查变量
  check_param(Rule) {
    this.ctx.validate(Rule);
  }


  async add() {
    const ctx = this.ctx;
    let count = ctx.cookies.get('count');
    count = count ? Number(count) : 0;
    ctx.cookies.set('count', ++count);
    ctx.body = count;
  }

  async remove() {
    const ctx = this.ctx;
    ctx.cookies.set('count', null);
    ctx.status = 204;
  }


  // app.passport.deserializeUser(async (ctx, user) => {
  //   // 处理 user
  //   // ...
  //   // return user;
  //   });
}
module.exports = BaseController;

